package cn.aran.basic.controller;

import cn.aran.basic.exception.BusinessException;
import cn.aran.basic.service.ILoginService;
import cn.aran.basic.utils.AjaxResult;
import cn.aran.basic.vo.LoginVo;
import cn.aran.basic.vo.WechatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginVo loginVo){
        try {
            Map<String,Object> map = loginService.accountLogin(loginVo);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PostMapping("/loginCode")
    public AjaxResult loginCode(@RequestBody LoginVo loginVo){
        try {
            Map<String,Object> map = loginService.loginCode(loginVo);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
    * @Title: wechat
    * @Description: 微信登录
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/10 17:19
    * @Parameters: [weChatVo]
    * @Return cn.aran.basic.utils.AjaxResult
    */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WechatVo wechatVo){
        try {
            return loginService.wechat(wechatVo);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
    * @Title: binder
    * @Description: 微信绑定并登录
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/12 14:13
    * @Parameters: [wechatVo]
    * @Return cn.aran.basic.utils.AjaxResult
    */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WechatVo wechatVo){
        try {
            Map<String,Object> map = loginService.binder(wechatVo);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }



}
